package org.xxpay.pay.channel.sqpay;

import org.springframework.stereotype.Service;
import org.xxpay.core.common.domain.api.AbstractRes;
import org.xxpay.core.common.domain.api.ApiBuilder;
import org.xxpay.core.common.domain.api.PayRes;
import org.xxpay.core.common.util.AmountUtil;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.common.vo.DBApplicationConfig;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayment;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: zhuxiao
 * @date: 18/8/9
 * @description: 双乾支付接口
 */
@Service
public class SqpayPaymentService extends BasePayment {

    private static final MyLog _log = MyLog.getLog(SqpayPaymentService.class);

    @Override
    public String getChannelName() {
        return SqpayConfig.CHANNEL_NAME_SQPAY;
    }

    /**
     * 支付
     * @param payOrder
     * @return
     */
    @Override
    public AbstractRes pay(PayOrder payOrder) {
        SqpayConfig sqpayConfig = new SqpayConfig(getPayParam(payOrder));
        PayRes retObj = ApiBuilder.buildSuccess(PayRes.class);
        String MD5key = sqpayConfig.getMD5key();
        String url = sqpayConfig.getReqUrl();
        String MerNo = sqpayConfig.getMerNo();                                                                                                           // 商户号
        String BillNo = payOrder.getPayOrderId();                                                                                                           // 商户订单号
        String Amount = AmountUtil.convertCent2Dollar(payOrder.getAmount()+"");                                    // 金额
        DBApplicationConfig dbConfig = getDBApplicationConfig();
        String ReturnURL = super.getReturnUrl(getChannelName(), dbConfig);                                                                 // 页面跳转同步通知页面
        String NotifyURL = super.getNotifyUrl(getChannelName(), dbConfig);                                                                   // 服务器异步通知路径

        String channelId = payOrder.getChannelId();
        String PayType;                                                                                                                                                           //CSPAY:个人网银支付 ; B2BPAY企业网银
        if(SqpayConfig.CHANNEL_NAME_SQPAY_B2C.equals(channelId)){
            PayType = "CSPAY";
        }else if(SqpayConfig.CHANNEL_NAME_SQPAY_B2B.equals(channelId)){
            PayType = "B2BPAY";
        }else{
            return ApiBuilder.bizError("不支持的渠道[channelId="+channelId+"]");
        }

        String MD5info = org.xxpay.pay.channel.sqpay.util.MD5Util.signMap(new String[]{MerNo, BillNo, Amount, ReturnURL}, MD5key, "REQ");

        // 支付参数
        Map<String, String> params = new HashMap<>();
        params.put("MerNo", MerNo);                    // 商户id
        params.put("BillNo", BillNo);                        // 订单号
        params.put("Amount", Amount);                // 金额
        params.put("ReturnURL", ReturnURL);      // 页面跳转同步通知页面
        params.put("NotifyURL", NotifyURL);        // 服务器异步通知路径
        params.put("PayType", PayType);              // 支付方式, B2C: B2C借记卡  快捷支付 B2B：企业网银支付
        params.put("MD5info", MD5info);             //参数加密串
        params.put("PaymentType", "");               //支付银行类型
        params.put("MerRemark", "");                   // 备注
        params.put("products", "");                        // 物品信息
        _log.info("[{}]下单请求参数:{}", getChannelName(), params);
        StringBuffer payForm = new StringBuffer();
        payForm.append("<form action=\""+url+"\" method=\"post\">");
        for(String k : params.keySet()) {
            payForm.append("<input type=\"text\" name=\""+k+"\" value=\""+params.get(k)+"\">");
        }
        payForm.append("<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >");
        payForm.append("</form>");
        payForm.append("<script>document.forms[0].submit();</script>");
        int result = commonService.payOrderService.updateStatus4Ing(payOrder.getPayOrderId(), null);
        _log.info("[{}]更新订单状态为支付中:payOrderId={},prepayId={},result={}", getChannelName(), payOrder.getPayOrderId(), "", result);
        // 支付链接地址
        retObj.setPayOrderId(payOrder.getPayOrderId()); // 设置支付订单ID
        retObj.setJumpInfo(payForm.toString());

        return retObj;
    }


}
